package com.example.backend.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.backend.api.common.DruidFactory;
import com.example.backend.api.common.Result;
import com.example.backend.api.entity.Employee;
import com.example.backend.api.entity.Project;
import com.example.backend.api.entity.Project_user;
import com.example.backend.api.mapper.EmployeeMapper;
import com.example.backend.api.mapper.ProjectMapper;
import com.example.backend.api.mapper.Project_userMapper;
import com.example.backend.api.service.Project_userService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xwyzsn
 * @since 2023-03-07
 */
@Service
public class Project_userServiceImpl extends ServiceImpl<Project_userMapper, Project_user> implements Project_userService {

    @Autowired
    private Project_userMapper projectUserMapper;

    @Data
    class ProjectUser{
        String project_id;
        Integer user_id;
        String name;
        String gender;
        String address;
        String tel;
        String age;

        public ProjectUser(String project_id, Integer user_id, String name, String gender, String address, String tel, String age) {
            this.project_id = project_id;
            this.user_id = user_id;
            this.name = name;
            this.gender = gender;
            this.address = address;
            this.tel = tel;
            this.age = age;
        }
    }
    public Result getUserDetail(String project_id) throws Exception {
        Connection connection = DruidFactory.getConnection();
        String sql = "SELECT * FROM project_user,employee WHERE project_id = '"+project_id+"'" + " and employee.user_id = project_user.user_id";
        System.out.println(sql);
        Statement statement = connection.createStatement();
        ResultSet resultSet = statement.executeQuery(sql);
        List<Object> list = new ArrayList<>();
        while(resultSet.next()){
            Integer user_id = resultSet.getInt("user_id");
            String name = resultSet.getString("name");
            String gender = resultSet.getString("gender");
            String address = resultSet.getString("address");
            String tel = resultSet.getString("tel");
            String age = resultSet.getString("birth");
            list.add(new ProjectUser(project_id,user_id,name,gender,address,tel,age));
        }
        statement.close();
        connection.close();
        return Result.success(list);
    }


    @Data
    private class Info{
        String code;
        Integer number;

        public Info(String code, Integer cnt) {
            this.code = code;
            this.number = cnt;
        }
    }
    public Result getInfo() throws Exception {
        Connection conn = DruidFactory.getConnection();
        String sql = "SELECT code,count(user_id) as total from project_user GROUP BY CODE";
        Statement statement = conn.createStatement();
        ResultSet  res = statement.executeQuery(sql);
        List<Object> list = new ArrayList<>();

        while(res.next()){

            String code = res.getString("code");
            Integer cnt = res.getInt("total");
            list.add(new Info(code,cnt));
        }
        statement.close();
        conn.close();
        return Result.success(list);
    }
    public Result getProjectUser(String project_id){
        QueryWrapper<Project_user> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("project_id",project_id);
        return Result.success(projectUserMapper.selectList(queryWrapper));
    }

    public Result getUser(String userId) {
        QueryWrapper<Project_user> queryWrapper =  new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        return Result.success(projectUserMapper.selectList(queryWrapper));
    }

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ProjectMapper projectMapper;
    @Data
    class PersonProject{
        private List<List<Employee>> employees;
        private List<Project> projectList;
    }
    public Result getPerson(Integer userId) {
        QueryWrapper<Project_user> queryWrapper =  new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        List<List<Employee>> list = new ArrayList<>();
        List<Project> projectList = new ArrayList<>();
        List<Project_user> projectUserList   = projectUserMapper.selectList(queryWrapper);
        for(Project_user project_user : projectUserList){
            Integer projectId = project_user.getProject_id();
            QueryWrapper<Project> projectQueryWrapper = new QueryWrapper<>();
            projectQueryWrapper.eq("project_num",projectId);
            Project project = projectMapper.selectOne(projectQueryWrapper);
            projectList.add(project);
            List<Employee> employees = new ArrayList<>();
            QueryWrapper<Project_user> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("project_id",projectId);
            List<Project_user> projectUserList1 = projectUserMapper.selectList(queryWrapper1);
            for(Project_user project_user1 : projectUserList1){
                Integer userId1 = project_user1.getUserId();
                if(Objects.equals(userId1, userId))
                    continue;
                QueryWrapper<Employee> queryWrapper2 = new QueryWrapper<>();
                queryWrapper2.eq("user_id",userId1);
                Employee employee = employeeMapper.selectOne(queryWrapper2);
                employees.add(employee);
            }
            list.add(employees);
        }
        PersonProject personProject = new PersonProject();
        personProject.setEmployees(list);
        personProject.setProjectList(projectList);
        return Result.success(personProject);
    }



}
